#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : lee

import sys
import os
import logging
import re
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class ModSubGroupNameCheck(CheckBase):
    def __init__(self):
        super(ModSubGroupNameCheck, self).__init__()

    def check_mod_group(self):
        """
        描述：检查mod组的正确性，同时检查top组的命名正确性
        """
        miss_node = []
        mod_group = "mod_grp"
        mod_subgroup_names = self.get_mod_subgroup_names()
        top_list = list(set(cmds.ls(assemblies=1)) - {'persp', 'top', 'front', 'side', 'Root_Grp'})
        check_list = top_list
        if not cmds.objExists(mod_group):
            check_list = top_list
        else:
            check_list += cmds.listRelatives(mod_group, c=1)
        for node in check_list:
            if node not in mod_subgroup_names:
                miss_node.append(node)
        # for node in cmds.listRelatives(mod_group, c=1):
            # 检查yeti生长面细分是否开启，默认应为开启
            # if node not in mod_subgroup_names:
            #     print node, mod_subgroup_names
            #     miss_node.append(node)

        return miss_node

    def get_mod_subgroup_names(self):
        # 通过文件名称获取资产信息
        maya_file = cmds.file(q=True, sn=True)
        file_name = os.path.basename(maya_file)
        asset_short_type = file_name.split("_")[0]
        asset_step_name = file_name.split("_")[2]
        asset_name = file_name.split("_")[1]

        mod_subgroup_names = []
        if asset_step_name in ['rig', 'mod']:
            mod_middle_names = ["render", "solverCloth", "furMesh", "deform", "solverCurves", "tracker", "guideCurve", "lowmod", "fxMesh"]
        elif asset_step_name in ['lowmod']:
            mod_middle_names = ['lowmod']
        else:
            mod_middle_names = []
        for name in mod_middle_names:
            group_name = "{0}_{1}_{2}_grp".format(asset_short_type, asset_name, name)
            mod_subgroup_names.append(group_name)
        return mod_subgroup_names

    def check(self):
        result_proc = self.check_mod_group()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

